package com.example.ymm.modules1.service.impl.sys.iot;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.sys.iot.IotMsLog;
import com.example.ymm.modules1.entity.my.sys.iot.IotTag;
import com.example.ymm.modules1.entity.my.sys.iot.ViewIotTag;
import com.example.ymm.modules1.mapper.my.sys.IotMsLogMapper;
import com.example.ymm.modules1.query.sys.iot.IotMsLogQueryCriteria;
import com.example.ymm.modules1.query.sys.iot.IotTagQueryCriteria;
import com.example.ymm.modules1.service.sys.iot.IotMsLogService;
import com.example.ymm.modules1.service.sys.iot.IotTagService;
import com.example.ymm.util.QueryHelpPlus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@Service
@DSTransactional
@DS("slave2")
@Slf4j
public class IotMsLogServiceImpl extends BaseServiceImpl<IotMsLogMapper, IotMsLog> implements IotMsLogService {


    @Autowired
    IotTagService iotTagService;

    @Override
    public TableList<IotMsLog> list(IotMsLogQueryCriteria criteria, YmPageDto pageDto) {
        IPage<IotMsLog> page = new Page<IotMsLog>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(IotMsLog.class, criteria);
        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<IotMsLog> tableList=new TableList(page);
        return tableList;
    }
    
    public List<IotMsLog> queryAll(IotMsLogQueryCriteria criteria) {
        List<IotMsLog> result =baseMapper.selectList(QueryHelpPlus.getPredicate(IotMsLog.class, criteria));
        return result;
    }

    @Override
    public JSONArray msLogDetail(String id) {
        IotMsLog msLog = getById(id);
        JSONArray jsonArray = JSONArray.parseArray(msLog.getTxt());
        List<BigInteger> houseLocationIdIn=new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            BigInteger houseLocationOID = jsonObject.getBigInteger("houseLocationOID");
            houseLocationIdIn.add(houseLocationOID);
        }

        IotTagQueryCriteria criteria=new IotTagQueryCriteria();
        criteria.setHouseLocationIdIn(houseLocationIdIn);
        List<ViewIotTag> iotTags = iotTagService.tableList(criteria);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            BigInteger houseLocationOID = jsonObject.getBigInteger("houseLocationOID");
            for (ViewIotTag iotTag : iotTags) {
                if (houseLocationOID.equals(iotTag.getHouseLocationId())) {
                    jsonObject.put("status", iotTag.getStatus());
                    jsonObject.put("tagMac", iotTag.getTagMac());
                    jsonObject.put("houseLocationName",iotTag.getHouseLocationName());
                }
            }
        }

        return jsonArray;
    }
}
